import Vue from 'vue'
import App from './App.vue'
// 引入ElementUI组件库
import ElementUI, { MessageBox, Message} from 'element-ui';
// 引入ElementUI全部样式
import 'element-ui/lib/theme-chalk/index.css';
//应用ElementUI
Vue.use(ElementUI);
//引入vue-router
import router from './router'
//引入样式
import './assets/less/index.less'
//引入store
import store from './store/index'
//引入axios
import http from 'axios'
//使用
Vue.prototype.$http = http
//引入Message
// import {Message} from 'element-ui'
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$message = Message

//引入mock
import '../api/mock.js'

router.beforeEach((to,from,next)=> {
  store.commit('getToken')
  const token = store.state.user.token
  console.log('token',token);
  if(!token && to.name !== 'login') {
    next({
      path: "/login"
    })
  }
  else if(token && to.name === 'login') {
    next({
      path: "/home"
    })
  }
  else {
    next()
  }
})

Vue.config.productionTip = false

new Vue({
  store,
  router,
  render: h => h(App),
  created() {
    store.commit('addMenu',router)
  }
}).$mount('#app')
